#version 130
uniform vec3 axisX;
uniform vec3 axisY;
uniform vec3 oriPos;
uniform vec3 lightPos;
uniform float halfDecay;
uniform float fullDecay;
uniform bool cut;
uniform float cutOff;
uniform vec3 dir;
uniform vec3 color;
uniform vec3 surfaceNormal;
uniform samplerCube visMap;
uniform vec4 surface_id;
uniform bool decay = false;
in vec3 norm1;
in vec3 norm2;
in vec3 norm3;
centroid in vec2 coord;
out vec4 fragResult0, fragResult1, fragResult2;
void main()
{
	vec3 pos = oriPos + axisX*coord.x + axisY*coord.y;
	vec3 incident = lightPos - pos;
	float dist = length(incident);
	incident /= dist;
	float decayFactor = (decay)?(dist>halfDecay?(fullDecay-dist)/(fullDecay-halfDecay)*0.5:(halfDecay-dist)/halfDecay*0.5+0.5):1.0;
	decayFactor = clamp(decayFactor,0.0,1.0);
	
	vec3 l1 = color * max(0.0,dot(incident,normalize(norm1)));
	l1 *= decayFactor;
	
	vec3 l2 = color * max(0.0,dot(incident,normalize(norm2)));
	l2 *= decayFactor;
	
	vec3 l3 = color * max(0.0,dot(incident,normalize(norm3)));
	l3 *= decayFactor;
	
	
	vec4 sid = texture(visMap, -incident);
	bool visible = false;
	visible = (sid == surface_id);

	float vis = 0.0;
	if (visible)
		vis = 1.0;

	fragResult0 = vec4(l1*vis, 0.0);
	fragResult1 = vec4(l2*vis, 0.0);
	fragResult2 = vec4(l3*vis, 0.0);
}